<template>
	<div class="admin">
		<el-card class="!border-none" shadow="never">
			<el-form class="mb-[-16px]" :model="formData" inline>
				<el-form-item label="物料信息">
					<el-input v-model="formData.title"  @keyup.enter="resetPage" class="w-40" size="small" clearable />
				</el-form-item>
				<!-- <el-form-item label="物料编码">
					<el-input v-model="formData.no" @keyup.enter="resetPage"  class="w-40" size="small" clearable />
				</el-form-item> -->
				<el-form-item label="物料分类">
					<el-tree-select class="w-40" @keyup.enter="resetPage"  :check-strictly="true" default-expand-all size="small"
						v-model="formData.cate_id" :data="options.jobss" :props="{ value: 'id', label: 'name'}" clearable multiple>
					</el-tree-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="resetPage" size="small">查询</el-button>
					<el-button @click="resetParams" size="small">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card v-loading="pager.loading" class="mt-2 !border-none" shadow="never">
			<el-button v-perms="['material.product/add']" type="primary" @click="handleAdd" size="small">
				<template #icon>
					<icon name="el-icon-Plus" />
				</template>
				新增物料
			</el-button>
			<div style="margin-top: 3px;">
				<mtable :data="pager.lists" :type="typeTable" :list="options.tableTile" :bill="false" :operation="true"
					:tableheight="tableheight" @handleAdds="handleAdds">
					<template #tool="{item}">
						<el-table-column v-if="item.display==1" sortable :label="item.name" show-overflow-tooltip
							:prop="item.value" :width="item.width">
							<template #default="{ row }">
								<div v-if="item.name=='计量单位'&&row.category_id==15">
									<span>{{row.unit_name_15}}</span>
									<span v-if="row.unit_name_15">/</span>
									<span>{{row.unit_name}}</span>
								</div>
								<div v-if="item.name=='计量单位'&&row.category_id!==15">
									<span>
										{{row.unit_name}}
									</span>
								</div>
								<div v-if="item.name=='从属设备'">
									<el-button @click="onHovertitles(row)" v-if="row.slave_name" size="small">
										{{row.slave_name}}
									</el-button>
								</div>
								<div v-if="item.name=='附件'&&row.product_image_name.length!=0">
									<div class="demo-image__preview">
										<el-button type="primary" plain @click="onenclosure(row.product_image_name)"
											size="small">
											查看
										</el-button>
									</div>
								</div>
								<div v-if="item.name=='图片'&&row.certificate_image.length!=0">
									<div class="demo-image__preview">
										<el-icon :size="24" :color="'#F9C068'">
											<PictureFilled style="cursor: pointer;" @click="onshowimg(row)" />
										</el-icon>
										<!-- <el-image class="imgTable"  :src="row.certificate_image[0]"
											:preview-src-list="row.certificate_image" :initial-index="0">
											<template #error>
												<div class="image-slot"></div>
											</template>
										</el-image> -->
									</div>
								</div>

							</template>
						</el-table-column>
					</template>
					<template #operation="{row}">
						<el-button v-perms="['material.product/edit']" type="primary" link @click="handleEdit(row)">
							编辑
						</el-button>
						<el-button v-if="row.root != 1" v-perms="['material.product/delete']" type="danger" link
							@click="handleDelete(row.id)">
							删除
						</el-button>
					</template>

				</mtable>
			</div>
			<div class="flex  justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<tableHover v-if="Hovertitles" ref="HovertitleRefs" @success="getLists" @close="Hovertitles = false" />
		<edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
		<EdittablePopup v-if="showTable" ref="editRefs" :type="typeTable" @success="getOptions"
			@close="showTable=false" />
		<enclosure v-if="enclosureShow" ref="enclosureRef" @success="getLists" @close="enclosureShow = false" />
		<imgbig v-if="showimg" ref="imgRef" @success="getLists" @close="showimg = false" />
	</div>
</template>

<script lang="ts" setup>
	import {
		adminLists,
		adminDelete
	} from '@/api/materials/product'
	import mtable from '@/components/table/index.vue'
	import {
		usePaging
	} from '@/hooks/usePaging'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'
	import {
		getCommonFieldDisplay
	} from '@/api/tools/code'
	import {
		adminListssmaterials
	} from '@/api/materials/list'
	import imgbig from '@/components/imgbig/index.vue'
	import enclosure from '@/components/enclosure/index.vue'
	import tableHover from '@/components/table-hovers/index.vue'
	import EdittablePopup from '@/components/settable/index.vue'
	const HovertitleRefs = shallowRef < InstanceType < typeof EditPopup >> ()
	const editRefs = shallowRef < InstanceType < typeof EdittablePopup >> ()
	const editRef = shallowRef < InstanceType < typeof EditPopup >> ()
	const enclosureRef = shallowRef < InstanceType < typeof enclosure >> ()
	const imgRef = shallowRef < InstanceType < typeof imgRef >> ()
	const typeTable = ref('product')
	const Hovertitles = ref(false)
	const showTable = ref(false)
	const enclosureShow = ref(false)
	const showimg = ref(false)
	// 表单数据
	const formData = reactive({
		title: '',
		no: '',
		cate_id: ''
	})
	const handleAdds = async () => {
		showTable.value = true
		await nextTick()
		editRefs.value?.open('add')
	}
	const onshowimg = async (img: any) => {
		
		showimg.value = true
		await nextTick()
		imgRef.value?.open('add')
		imgRef.value?.setFormData(img)
	}
	const showEdit = ref(false)
	const {
		pager,
		getLists,
		resetParams,
		resetPage
	} = usePaging({
		fetchFun: adminLists,
		params: formData
	})
	const options = reactive({
		jobss: [] as any[],
		tableTile: [] as any[]
	})

	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
	}

	const onenclosure = async (val: any) => {
		
		enclosureShow.value = true
		await nextTick()
		enclosureRef.value?.open('add')
		enclosureRef.value?.setFormData(val)
	}
	watch(() => pager.lists, (newValue, oldValue) => {
		for (let i = 0; i < newValue.length; i++) {
			for (let j = 0; j < newValue[i].cate_name.length; j++) {
				if (newValue[i].cate_name[j].id == newValue[i].category_id) {
					newValue[i].cate_names = newValue[i].cate_name[j].name
				}
			}
		}
	}, {
		deep: true
	})
	const onHovertitles = async (data: any) => {
		Hovertitles.value = true
		await nextTick()
		HovertitleRefs.value?.open('edit')
		HovertitleRefs.value?.setFormData(data)
	}
	const getOptions = () => {
		getCommonFieldDisplay({
			type: typeTable.value
		}).then((data: any) => {
			options.tableTile = data
		})
		adminListssmaterials({
			page_type: 0
		}).then((data: any) => {
			options.jobss = data
		})
	}
	const tableheight = ref(null)
	const heighting = () => {
		tableheight.value = parseInt(document.documentElement.clientHeight) - 180
	}
	const handleEdit = async (data: any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
	}
	const onAll = () => {
		resetParams()
	}

	const handleDelete = async (id: number) => {
		await feedback.confirm('确定要删除？')
		await adminDelete({
			id
		})
		getLists()
	}

	onMounted(() => {
		heighting()
		getLists()
		getOptions()
	})
</script>
<style scoped lang="scss">

</style>
